﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using WebAPI.Service1.Model;
using WebAPI.Service1.Entity;

namespace WebAPI.Service1.DAL
{
    public class TestDAL
    {
        public string Insert(RequestEntity entity)
        {
            string errMsg = "";
            erpTestEntity erpTestEntities = new erpTestEntity();
            try
            {
                k_CstTouch testEntity = new k_CstTouch()
                {
                    TaskGUID = Guid.Parse(entity.TaskGUID),
                    TalkContent = entity.TalkContent,
                    Lxr = entity.Lxr,
                    LxGUID = Guid.Parse(entity.LxGUID),
                    Djr = entity.Djr,
                    LxDate = DateTime.Parse(entity.LxDate)

                };

                erpTestEntities.Entry<k_CstTouch>(testEntity).State = System.Data.Entity.EntityState.Added;
                erpTestEntities.SaveChanges();
                errMsg += "成功";
            }
            catch (Exception ex)
            {
                errMsg += ex.Message;
            }
            return errMsg;
        }

        public string Delete(RequestEntity entity)
        {
            string errMsg = "";
            erpTestEntity erpTestEntities = new erpTestEntity();
            try
            {
                k_CstTouch testEntity = new k_CstTouch()
                {
                    LxGUID = Guid.Parse(entity.LxGUID)
                };

                erpTestEntities.Entry<k_CstTouch>(testEntity).State = System.Data.Entity.EntityState.Deleted;
                erpTestEntities.SaveChanges();
                errMsg += "成功";
            }
            catch (Exception ex)
            {
                errMsg += ex.Message;
            }
            return errMsg;
        }

        public string Update(RequestEntity entity)
        {
            string errMsg = "";
            erpTestEntity erpTestEntities = new erpTestEntity();
            try
            {
                k_CstTouch testEntity = new k_CstTouch()
                {
                    LxGUID = Guid.Parse(entity.LxGUID),
                    TaskGUID = Guid.Parse(entity.TaskGUID),
                    LxDate = DateTime.Parse(entity.LxDate),
                    Djr = entity.Djr,
                    Lxr = entity.Lxr,
                    TalkContent = entity.TalkContent
                };

                erpTestEntities.Entry<k_CstTouch>(testEntity).State = System.Data.Entity.EntityState.Modified;
                erpTestEntities.SaveChanges();
                errMsg += "成功";
            }
            catch (Exception ex)
            {
                errMsg += ex.Message;
            }
            return errMsg;
        }

        public string Select(RequestEntity entity)
        {
            //string jsonData = "";
            erpTestEntity erpTestEntities = new erpTestEntity();
            try
            {
                var test = erpTestEntities.k_CstTouch.Where(u => string.IsNullOrEmpty(entity.LxGUID) || entity.LxGUID.Contains(u.LxGUID.ToString())).Select(u =>  new { u.LxGUID, u.TaskGUID, u.LxDate, u.Lxr,  u.TalkContent, u.Djr }).ToList();
                if(test.Count != 0)
                {
                    return JsonConvert.SerializeObject(test);
                }
                else
                {
                    return "没有资料！";
                }
                //jsonData = string.Join(",", test);
            }
            catch (Exception ex)
            {
                return ex.ToString();
                //jsonData += ex.Message;
            }

            //return jsonData;
        }

        public string SelectAll(RequestEntity entity)
        {
            //string jsonData = "";
            erpTestEntity erpTestEntities = new erpTestEntity();
            try
            {
                var test = erpTestEntities.k_CstTouch.Where(u => 1 == 1).Select(u => new { u.LxGUID, u.TaskGUID, u.LxDate, u.Lxr, u.TalkContent, u.Djr }).ToList();
                return JsonConvert.SerializeObject(
                    test
                );
                //jsonData = string.Join(",", test);
            }
            catch (Exception ex)
            {
                return ex.ToString();
                //jsonData += ex.Message;
            }

            //return jsonData;
        }
    }
}
